package com.ziningmei.demo.framework.helper;

import com.ziningmei.demo.framework.Util.ArrayUtil;
import com.ziningmei.demo.framework.Util.CollectionUtil;
import com.ziningmei.demo.framework.Util.ReflectionUtil;
import com.ziningmei.demo.framework.annotation.Inject;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/10.
 */
public class IocHelper {
    static {
        Map<Class<?>, Object> beanMap = BeanHelper.getBeanMap();
        if (CollectionUtil.isNotEmpty(beanMap)) {
            for (Map.Entry<Class<?>, Object> beanEntity : beanMap.entrySet()) {
                Class<?> beanClass=beanEntity.getKey();
                Object beanInstance = beanEntity.getValue();
                Field[] beanFields=beanClass.getDeclaredFields();
                if(ArrayUtil.isNotEmpty(beanFields)){
                    for(Field beanField:beanFields){
                        if(beanField.isAnnotationPresent(Inject.class)){
                            Class<?> type = beanField.getType();
                            Object beanFieldInstance = beanMap.get(type);
                            if(beanFieldInstance!=null){
                                ReflectionUtil.setField(beanInstance,beanField,beanFieldInstance);
                            }
                        }
                    }
                }

            }
        }
    }
}
